﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using TelnetRouterApplication.Control;

namespace TelnetRouterApplication.View
{
    public partial class ConnectForm : Form
    {
        private static bool _DEBUG = false;
        public static bool DEBUG
        {
            get { return ConnectForm._DEBUG; }
            set { ConnectForm._DEBUG = value; }
        }
        private MainForm _mainForm;
        public MainForm mainForm
        {
            get { return _mainForm; }
            set { _mainForm = value; }
        }

        private IpModel _ipModel;
        public IpModel IpModel
        {
            get { return _ipModel; }
            set { _ipModel = value; }
        }

        public ConnectForm() : this(null) { }

        public ConnectForm(MainForm mainForm)
        {
            this.mainForm = mainForm;
            InitializeComponent();
        }

        private void cancelBtn_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void cleanBtn_Click(object sender, EventArgs e)
        {
            ipAddressTx.Text = "";
            portTx.Text = "";
            passTx.Text = "";
        }

        private void connectBtn_Click(object sender, EventArgs e)
        {
            //Mando um false porque acabou de conectar
            string ipAddress = ipAddressTx.Text;
            int port = int.Parse(portTx.Text);

            if (DEBUG)
            {
                System.Diagnostics.Debug.WriteLine("Tentando conexão com "+ipAddress+" na porta "+port);
            }

            IpModel = new IpModel(ipAddress, port);

            bool connectSuccesfully = ConnectionHandler.GetInstance().Connect(IpModel);

            if (connectSuccesfully)
            {
                bool password = ConnectionHandler.GetInstance().NeedPassword();

                if (password)
                {
                    string passwordString = passTx.Text;
                    if (passwordString.Equals(""))
                    {
                        MessageBox.Show("Password required!");
                    }
                    else
                    {
                        password = false;

                        while (!password)
                        {
                            password = ConnectionHandler.GetInstance().SendPassword(passwordString);

                            if (password)
                            {
                                if (MessageBox.Show("Connection successfully.") == DialogResult.OK)
                                {
                                    RouterLevelHandler.GetInstance().setInitialState();

                                    finalize();
                                    this.Close();
                                }
                            }
                            else
                            {
                                InputBoxForm inputBox = new InputBoxForm("Wrong password. Please, try again.");
                                DialogResult dialog = inputBox.ShowDialog();
                                if (dialog == DialogResult.OK)
                                {
                                    passwordString = inputBox.MessageWritten;
                                }
                                else
                                {
                                    password = true;
                                    MessageBox.Show("Unable to connect. Please, try again.");
                                }
                            }
                        }
                    }
                    
                }
                else
                {
                    if (MessageBox.Show("Connection successfully.") == DialogResult.OK)
                    {
                        RouterLevelHandler.GetInstance().setInitialState();

                        finalize();
                        this.Close();
                    }
                }

            }
            else
            {
                MessageBox.Show("Unable to connect. Please, try again.");
            }
        }

        public void finalize()
        {
            mainForm.blockCommands(false);
            mainForm.changeStatus(IpModel);

        }
        
    }
}
